package Servlet;

import DAO.BaseJdbc;
import DAO.MessageDAO;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

@WebServlet(name= "allArticleServlet",value = "/allArticleServlet")
public class allArticleServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        int pageNo= Integer.parseInt(request.getParameter("pageNo"));
        System.out.println(pageNo);
        MessageDAO messageDAO = new MessageDAO();
        JSONArray json = null;
        try {
            json = JSONArray.fromObject(messageDAO.getArticleList(pageNo));
            JSONObject json1 = new JSONObject();
            json1.accumulate("totalPage", messageDAO.getPageCount2());
            json1.accumulate("total", messageDAO.geTotalPage2());
            json1.accumulate("pageSize",messageDAO.pageSize);
            json1.accumulate("page",pageNo);
            json.add(json1);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        System.out.println(json.toString());
        PrintWriter out = response.getWriter();
        out.print(json);
        out.flush();
        out.close();
        try {
            BaseJdbc.close(messageDAO.conn);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
}
